package com.specter.mvc.model;

import java.util.HashMap;
import java.util.Map;

/**
 * 响应对象
 *
 * @author Liang.Wang
 * @version 1.0
 */
public class Response {

	/** 响应头部 */
	private Meta meta;
	/** 响应内容 */
	private Object data;

	public static Response success() {
		return new Response(true, "OK");
	}

	public static Response success(String message) {
		return new Response(true, message);
	}

	public static Response failure(String message) {
		return new Response(false, message);
	}

	public Response data(Object data) {
		this.data = data;// 重复调用会覆盖值
		return this;
	}

	@SuppressWarnings("unchecked")
	public Response data(String key, Object value) {
		if (!(data instanceof Map)) {
			this.data = new HashMap<String, Object>();// 非Map类型重复调用会覆盖值
		}
		((Map<String, Object>) this.data).put(key, value);

		return this;
	}

	private Response(boolean success, String message) {
		this.meta = new Meta(success, message);
	}

	public Meta getMeta() {
		return meta;
	}

	public Object getData() {
		return data;
	}

	public class Meta {
		/** 状态 */
		private boolean success;
		/** 消息 */
		private String message;

		public Meta(boolean success) {
			this.success = success;
		}

		public Meta(boolean success, String message) {
			this.success = success;
			this.message = message;
		}

		public boolean isSuccess() {
			return success;
		}

		public String getMessage() {
			return message;
		}
	}
}
